package com.dom.common.generator;

import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class CodeGenerator {
    // 演示例子，执行 main 方法控制台输入模块表名回车自动生成对应项目目录中

    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        //TODO 变更项目模块时修改
        gc.setOutputDir(projectPath + "/dom-stor/src/main/java");
        gc.setAuthor("xllei");
        gc.setOpen(false);
        gc.setFileOverride(true);
        gc.setActiveRecord(false);// 不需要ActiveRecord特性的请改为false
        gc.setEnableCache(false);// XML 二级缓存
        gc.setBaseResultMap(true);// XML ResultMap
        gc.setBaseColumnList(true);// XML columList
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://47.105.191.221:3306/invodb?useUnicode=true&useSSL=false&characterEncoding=utf8");
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("invo");
        dsc.setPassword("Invo@2018!");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
//        pc.setModuleName(scanner("包名"));
        pc.setModuleName("");
        //TODO 变更包路径时修改
        pc.setParent("com.dom.stor.dao");
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        List<FileOutConfig> focList = new ArrayList<>();
//        focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
//            @Override
//            public String outputFile(TableInfo tableInfo) {
//                // 自定义输入文件名称
//                return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
//                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
//            }
//        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
//        mpg.setTemplate(new TemplateConfig().setXml(null));

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(false);
        strategy.setVersionFieldName("version");
//        strategy.setControllerMappingHyphenStyle(true);//生成controller控制器
//        strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
//        strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");
//        strategy.setInclude(scanner("tc_batch_transfer_flow"));
        //TODO 变更生成db对象时修改
        strategy.setInclude(new String[]{"ts_stor_goods_detail_info", "ts_stor_goods_info"});
//        strategy.setInclude(new String[]{"t_client_cont_info", "t_client_info", "t_emp_info", "t_emp_sala_info", "t_good_standard_tbl", "t_good_variety_tbl", "t_goods_info", "t_goods_limit_conf", "t_measure_unit_tbl", "t_member_comp_info", "t_menu_info", "t_organ_info", "t_role_info", "t_role_priv_rel", "t_stor_cont_info", "t_stor_goods_detail_info", "t_stor_goods_info", "t_stor_info", "t_supp_info", "t_sys_code_tbl", "t_sys_logs", "t_sys_param", "t_sys_sequence", "t_trade_date", "t_trade_param", "t_user_info", "t_verfy_rule_tbl", "tm_market_back_detail_flow", "tm_market_back_flow", "tm_market_order_detail_flow", "tm_market_order_flow", "tm_market_sale_detail_flow", "tm_market_sale_flow", "tp_purc_app_flow", "tp_purc_app_detail_flow", "tp_purc_back_detail_flow", "tp_purc_back_flow", "tp_purc_cred_detail_flow", "tp_purc_cred_flow", "tp_purc_order_flow", "tp_purc_order_detail_flow"});
//        strategy.setInclude(new String[]{
//                "ts_bind_detail_flow",
//                "ts_combind_flow",
//                "ts_dama_detail_flow",
//                "ts_dama_order_flow",
//                "ts_dismantle_detail_flow",
//                "ts_inventory_detail_flow",
//                "ts_inventory_flow",
//                "ts_secondment_detail_flow",
//                "ts_secondment_flow",
//                "ts_stock_in_detail_flow",
//                "ts_stock_in_flow",
//                "ts_stock_out_detail_flow",
//                "ts_stock_out_flow",
//                "ts_stor_allot_detail_flow",
//                "ts_stor_allot_flow",
//                "ts_stor_msg_flow"
//        });
//        strategy.setInclude(new String[]{
//                "t_client_cont_info",
//                "t_client_info",
//                "t_emp_info",
//                "t_emp_sala_info",
//                "t_good_standard_tbl",
//                "t_good_variety_tbl",
//                "t_goods_info",
//                "t_goods_limit_conf",
//                "t_measure_unit_tbl",
//                "t_member_comp_info",
//                "t_menu_info",
//                "t_organ_info",
//                "t_role_info",
//                "t_role_priv_rel",
//                "t_stor_cont_info",
//                "t_stor_goods_detail_info",
//                "t_stor_goods_info",
//                "t_stor_info",
//                "t_supp_info",
//                "t_sys_code_tbl",
//                "t_sys_logs",
//                "t_sys_param",
//                "t_sys_sequence",
//                "t_trade_date",
//                "t_trade_param",
//                "t_user_info",
//                "t_verfy_rule_tbl"
//        });
//        strategy.setInclude(new String[]{"tp_purc_app_detail_flow", "tp_purc_cred_flow", "tp_purc_order_detail_flow"});
//        strategy.setTablePrefix(pc.getModuleName() + "_");
        mpg.setStrategy(strategy);
//        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }

}
